package com.pz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.pz.gulimall.product.entity.ProductAttrValueEntity;
import com.pz.gulimall.product.service.impl.AttrServiceImpl;
import com.pz.gulimall.product.vo.req.AttrReqVo;
import com.pz.gulimall.product.vo.res.AttrResVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.product.service.AttrService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-01 20:22:27
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Resource
    private AttrServiceImpl attrServiceImpl;


    @GetMapping("/test")
    public R testGateway(){
        return R.ok("success");
    }

    /**
     * 获取spu规格
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> list = attrService.getAttrValueByspuId(spuId);
        return R.ok().put("data", list);
    }

    @PostMapping("/update/{spuId}")
    public R updateAttrValusBuspuId(@RequestBody List<ProductAttrValueEntity> entities,@PathVariable Long spuId){
        attrService.updateAttrValueBySpuId(entities, spuId);
        return R.ok();
    }


    /**
     * 列表
     * @param params    携带分页的基本参数和搜索条件 "key"
     * @param attrType  sale-销售属性（不存在分组），base-基本属性
     * @param catId     0--查所有；非0精确查询
     * @return
     */
    @RequestMapping("/{attrType}/list/{attrId}")
    public R listBase(@RequestParam Map<String, Object> params,
                      @PathVariable("attrType")String attrType ,
                      @PathVariable("attrId")Long catId){
        PageUtils page = attrServiceImpl.queryPage(params, catId, attrType);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResVo attr = attrService.getAttrById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrReqVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrReqVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
